package org.feng.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;

import lombok.AllArgsConstructor;
import org.feng.entity.Menu;
import org.feng.base.entity.Result;
import org.feng.service.MenuService;
import org.feng.utils.ExcelUtils;
import org.feng.utils.TreeUtil;
import org.feng.utils.condition.Condition;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * <p>
 * 菜单表
 * </p>
 *
 * @author feng
 * @since 2020-12-09
 */
@RestController
@RequestMapping("/system/menu")
@AllArgsConstructor
public class MenuController {
    private final MenuService menuService;

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelUtils.writeExcel(response, menuService.list(), Menu.class);
    }

    /**
     * 创建按钮组
     */
    @PostMapping("/createBtn")
    public Result<Boolean> createBtn(@RequestBody List<Menu> list) {
        return Result.status(menuService.createBtn(list));
    }

    /**
     * 菜单树
     */
    @GetMapping("/treeList")
    public Result<List<Menu>> treeList() {
        return Result.success(TreeUtil.tree(menuService.treeList()));
    }

    /**
     * 菜单树
     */
    @GetMapping("/menuTreeList")
    public Result<List<Menu>> menuTreeList() {
        return Result.success(TreeUtil.tree(menuService.list(Condition.getQueryWrapper(null, "sort", "asc"))));
    }


    /**
     * 列表
     */
    @GetMapping
    public Result<List<Menu>> list() {
        return Result.success(menuService.list());
    }

    /**
     * 分页列表
     */
    @GetMapping(value = "/page")
    public Result<IPage<Menu>> page() {
        return Result.success(menuService.page(Condition.getPage()));
    }

    /**
     * 详情
     */
    @GetMapping(value = "/query")
    public Result<Menu> detail(@RequestParam Long id) {
        return Result.success(menuService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody Menu menu) {
        return Result.status(menuService.save(menu));
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody Menu menu) {
        return Result.status(menuService.updateById(menu));
    }

    /**
     * 删除
     */
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(menuService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
